package com.qeegoo.b2bautozityp.moudle.good.viewmodel;

import com.orhanobut.logger.Logger;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.api.entity.HttpResult;
import com.qeegoo.b2bautozityp.moudle.good.model.CarModelBean;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsPropertyBean;
import com.qeegoo.b2bautozityp.moudle.goods.model.GoodsDetailResult;
import com.qeegoo.b2bautozityp.rxbus.IRxBusListener;
import com.qeegoo.b2bautozityp.rxbus.RxBus;
import com.qeegoo.b2bautozityp.rxbus.event.GoodsEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import base.lib.util.ActivityManager;
import base.lib.util.ToastUtils;
import rx.Subscription;
import rx.functions.Action1;

/**
 * 王少岩 在 2017/9/14 创建了它
 */

public class GoodsViewModel implements IRxBusListener {
    private RxBus mRxBus;
    private RequestApi mRequestApi;
    private GoodsPropertyBean mPropertyBean;
    private GoodsDetailResult mDetailResult;
    private String mGoodsId;
    private ArrayList<CarModelBean> selectBeans = new ArrayList<>();

    public GoodsViewModel(RequestApi requestApi, RxBus rxBus) {
        mRxBus = rxBus;
        mRequestApi = requestApi;
    }

    public GoodsPropertyBean getPropertyBean() {
        return mPropertyBean;
    }

    public GoodsDetailResult getDetailResult() {
        return mDetailResult;
    }

    public void getGoodsPropertys(String goodsId) {
        mRequestApi.getGoodsProperty().compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<GoodsPropertyBean>() {
                    @Override
                    public void onNext(GoodsPropertyBean goodsPropertyBean) {
                        mPropertyBean = goodsPropertyBean;
                        mRxBus.post(new GoodsEvent(GoodsEvent.FLAG_PROPERTY_COMPLETE));
                        getGoodsDetails(goodsId);
                    }
                });
    }

    private void getGoodsDetails(String goodsId) {
        mGoodsId = goodsId;
        mRequestApi.detailGoods(HttpParams.paramDetailById(goodsId))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<GoodsDetailResult>() {
                    @Override
                    public void onNext(GoodsDetailResult goodsDetailResult) {
                        mDetailResult = goodsDetailResult;
                        if (goodsDetailResult.carSeries != null)
                            selectBeans.addAll(goodsDetailResult.carSeries);
                        mRxBus.post(new GoodsEvent(GoodsEvent.FLAG_DETAIL_COMPLETE));
                    }
                });
    }

    public void saveGoods(GoodsInfoViewModel infoViewModel, GoodsPriceViewModel priceViewModel, String status) {
        Map<String, String> params = new HashMap<>();
        params.put("id", mGoodsId);
        params.put("status", status);
        StringBuilder sb_mids = new StringBuilder();
        String mids = "";
        for (CarModelBean bean : selectBeans) {
            sb_mids.append(bean.id).append(",");
        }
        if (sb_mids.length() > 0) {
            mids = sb_mids.substring(0, sb_mids.length() - 1);
        }
        if (infoViewModel.commonPic.get() == R.mipmap.ic_switch_open) {
            mids = "";
        }
        params.put("mids", mids);
        infoViewModel.getBaseInfoParam(params);
        if (params.size() == 0)
            return;
        priceViewModel.getPriceInfoParam(params);
        if (params.size() == 0)
            return;
        mRequestApi.saveGoods(params)
                .compose(RetrofitService.applySchedulers())
                .subscribe(new ProgressSubscriber<HttpResult>() {
                    @Override
                    public void onNext(HttpResult httpResult) {
                        ToastUtils.showToast(httpResult.getStatus().getMsg());
                        if (httpResult.getStatus().isSuccess())
                            ActivityManager.getActivity().finish();
                    }
                });
    }

    public ArrayList<CarModelBean> getSelectBeans() {
        return selectBeans;
    }

    @Override
    public <T> void registerRxBus(Class<T> eventType, Action1<T> action) {
        Subscription subscription = mRxBus.doSubscribe(eventType, action, throwable -> Logger.e(throwable.toString()));
        mRxBus.addSubscription(this, subscription);
    }

    @Override
    public void unregisterRxBus() {
        mRxBus.unSubscribe(this);
    }
}
